﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CApJVLEditorViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using EngineeringInternalExtension.CommandParameters;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using UiInternalControls.WindowsFormsControls;
using UiInternalControls.WindowsFormsControls.GraphEnums;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CApJVLEditorViewModel : IDisposable
{
  private const int GRAPH_PADDING = 5;
  private const double GRAPH_RANGE_EPSILON = 1E-06;
  private const double GRAPH_RANGE_REVOLUTE_MIN_DEG = -180.0;
  private const double GRAPH_RANGE_REVOLUTE_MAX_DEG = 180.0;
  private const double GRAPH_RANGE_PRISMATIC_MIN_MM = -100.0;
  private const double GRAPH_RANGE_PRISMATIC_MAX_MM = 100.0;
  private const double PRISMATIC_LIMIT_VALUE = 999999.0;
  private const double REVOLUTE_LIMIT_VALUE = 17453.27507;
  private const string SETTINGS_GRAPH_PATH = "GraphSettings";
  private const string SETTINGS_GRID_PATH = "GridSettings";
  private const string SETTINGS_GENERAL_PATH = "FormSettings";
  private const string SETTINGS_GRAPH_PROPERTY_REGULAR_POINT_SHAPE = "RegularPointShape";
  private const string SETTINGS_GRAPH_PROPERTY_REGULAR_POINT_COLOR = "RegularPointColor";
  private const string SETTINGS_GRAPH_PROPERTY_SELECTED_POINT_SHAPE = "SelectedPointShape";
  private const string SETTINGS_GRAPH_PROPERTY_SELECTED_POINT_COLOR = "SelectedPointColor";
  private const string SETTINGS_GRAPH_PROPERTY_LINE_TYPE = "LineType";
  private const string SETTINGS_GRAPH_PROPERTY_LINE_COLOR = "LineColor";
  private const string SETTINGS_GRAPH_PROPERTY_AREA_COLOR = "AreaColor";
  private const string SETTINGS_GRID_PROPERTY_N_COLUMN_WIDTH_RATIO = "NColumnWidthRatio";
  private const string SETTINGS_GRID_PROPERTY_X_COLUMN_WIDTH_RATIO = "XColumnWidthRatio";
  private const string SETTINGS_GRID_PROPERTY_Y_COLUMN_WIDTH_RATIO = "YColumnWidthRatio";
  private const string SETTING_GENERAL_SPLITTER_POS_RATIO = "SplitterPosRatio";
  private const int GRID_N_COLUMN_MIN_WIDTH = 20;
  private const int GRID_X_COLUMN_MIN_WIDTH = 75;
  private const int GRID_Y_COLUMN_MIN_WIDTH = 85;
  private const int MAX_ITEMS_COUNT = 100;
  private CApJVLEditorModel _model;
  private object _ioData;
  private List<Control> _connectedControls;
  private double _revoluteUnitsMultiplier;
  private int _revoluteDigitsAfterDecimalPoint;
  private string _revoluteUnitsName;
  private double _prismaticUnitsMultiplier;
  private int _prismaticDigitsAfterDecimalPoint;
  private string _prismaticUnitsName;
  private CApJVLEditorViewModel.eDataType _xDataType;
  private CApJVLEditorViewModel.eDataType _yDataType;
  private double _graphDefaultMinX;
  private double _graphDefaultMaxX;
  private double _graphDefaultMinY;
  private double _graphDefaultMaxY;
  private int _updateCounter;
  private int _internalModelUpdateCounter;
  private int _internalSelectionUpdateCounter;
  private ITxGraphPoint _focusItem;
  private CUiJVLEditorViewModelSettings _settings;

  internal CApJVLEditorViewModel(object ioData)
  {
    this._model = new CApJVLEditorModel();
    this._ioData = ioData;
    this._connectedControls = new List<Control>(2);
    this._settings = new CUiJVLEditorViewModelSettings();
    this._revoluteUnitsMultiplier = TxApplication.Options.Units.AngularMultiplier;
    this._revoluteDigitsAfterDecimalPoint = TxApplication.Options.Units.AngularDigitsAfterDecimalPoint;
    this._revoluteUnitsName = TxApplication.Options.Units.AngularUnitName;
    this._prismaticUnitsMultiplier = TxApplication.Options.Units.LinearMultiplier;
    this._prismaticDigitsAfterDecimalPoint = TxApplication.Options.Units.LinearDigitsAfterDecimalPoint;
    this._prismaticUnitsName = TxApplication.Options.Units.LinearUnitName;
    this.IO_initModelInfo();
    this.IO_initModelData();
    this.registerToModelEvents();
  }

  public void Dispose() => this.unregisterToModelEvents();

  internal void Connect(Control control)
  {
    switch (control)
    {
      case CUiJVLEditorGraph _:
        this.connectGraph(control as CUiJVLEditorGraph);
        break;
      case CUiJVLEditorGrid _:
        this.connectGrid(control as CUiJVLEditorGrid);
        break;
    }
  }

  internal void Disconnect(Control control)
  {
    switch (control)
    {
      case CUiJVLEditorGraph _:
        this.disconnectGraph(control as CUiJVLEditorGraph);
        break;
      case CUiJVLEditorGrid _:
        this.disconnectGrid(control as CUiJVLEditorGrid);
        break;
    }
  }

  internal bool ValidConnectedControls
  {
    get
    {
      foreach (Control connectedControl in this._connectedControls)
      {
        if (connectedControl is CUiJVLEditorGrid && !(connectedControl as CUiJVLEditorGrid).IsValid)
          return false;
      }
      return true;
    }
  }

  private void beginUpdateControls()
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGraph)
        this.beginUpdateGraph(connectedControl as CUiJVLEditorGraph);
      else if (connectedControl is CUiJVLEditorGrid)
        this.beginUpdateGrid(connectedControl as CUiJVLEditorGrid);
    }
  }

  private void endUpdateControls()
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGraph)
        this.endUpdateGraph(connectedControl as CUiJVLEditorGraph);
      else if (connectedControl is CUiJVLEditorGrid)
        this.endUpdateGrid(connectedControl as CUiJVLEditorGrid);
    }
  }

  internal void BeginUpdate()
  {
    ++this._updateCounter;
    if (this._updateCounter != 1)
      return;
    this.beginUpdateControls();
    this.fireUpdateStarted();
  }

  internal void EndUpdate()
  {
    if (this._updateCounter <= 0)
      return;
    --this._updateCounter;
    if (this._updateCounter != 0)
      return;
    this.endUpdateControls();
    this.fireUpdateEnded();
  }

  internal bool Updating => this._updateCounter > 0;

  internal event EventHandler UpdateStarted;

  internal event EventHandler UpdateEnded;

  internal event CApJVLEditorViewModel_ListChangedEventHandler ListChanged;

  internal event CApJVLEditorViewModel_ListItemPropertyChangedEventHandler ListItemPropertyChanged;

  internal event CApJVLEditorViewModel_SelectionChangedEventHandler SelectionChanged;

  internal event CApJVLEditorViewModel_FocusItemChangedEventHandler FocusItemChanged;

  internal ITxGraphPoint CreatePoint(double x, double y) => this._model.CreatePoint(x, y);

  internal bool IsValidXValue(double x, out string errorMsg)
  {
    bool flag = true;
    errorMsg = (string) null;
    double minValue;
    double maxValue;
    this.getValidRange(this._xDataType, out minValue, out maxValue);
    if (x < minValue || x > maxValue)
    {
      string formatedValue1 = this.getFormatedValue(this._xDataType, minValue);
      string formatedValue2 = this.getFormatedValue(this._xDataType, maxValue);
      errorMsg = string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.ERR_VALUE_OUT_OF_RANGE, (object) formatedValue1, (object) formatedValue2);
      flag = false;
    }
    return flag;
  }

  internal bool IsValidYValue(double y, out string errorMsg)
  {
    bool flag = true;
    errorMsg = (string) null;
    double minValue;
    double maxValue;
    this.getValidRange(this._yDataType, out minValue, out maxValue);
    if (y < minValue || y > maxValue)
    {
      string formatedValue1 = this.getFormatedValue(this._yDataType, minValue);
      string formatedValue2 = this.getFormatedValue(this._yDataType, maxValue);
      errorMsg = string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.ERR_VALUE_OUT_OF_RANGE, (object) formatedValue1, (object) formatedValue2);
      flag = false;
    }
    return flag;
  }

  internal ITxGraphPoint this[int index] => ((Collection<ITxGraphPoint>) this._model)[index];

  internal int Count => ((Collection<ITxGraphPoint>) this._model).Count;

  internal int MaxCount => 100;

  internal bool Contains(ITxGraphPoint point)
  {
    return ((Collection<ITxGraphPoint>) this._model).Contains(point);
  }

  internal int IndexOf(ITxGraphPoint point)
  {
    return ((Collection<ITxGraphPoint>) this._model).IndexOf(point);
  }

  internal void Add(ITxGraphPoint point) => ((Collection<ITxGraphPoint>) this._model).Add(point);

  internal void AddRange(IEnumerable<ITxGraphPoint> points)
  {
    this.beginUpdateModel();
    foreach (ITxGraphPoint point in points)
      ((Collection<ITxGraphPoint>) this._model).Add(point);
    this.endUpdateModel();
  }

  internal void SetRange(IEnumerable<ITxGraphPoint> points)
  {
    if (((Collection<ITxGraphPoint>) this._model).Count == 0)
    {
      this.AddRange(points);
    }
    else
    {
      this.beginUpdateModel();
      this.RemoveAll();
      this.AddRange(points);
      this.endUpdateModel();
    }
  }

  internal void InsertAt(int index, ITxGraphPoint point)
  {
    ((Collection<ITxGraphPoint>) this._model).Insert(index, point);
  }

  internal void InsertRange(int index, IEnumerable<ITxGraphPoint> points)
  {
    if (index < 0 || index > this.Count)
      throw new ArgumentOutOfRangeException(nameof (index));
    if (index == this.Count)
    {
      this.AddRange(points);
    }
    else
    {
      this.beginUpdateModel();
      int index1 = index;
      foreach (ITxGraphPoint point in points)
      {
        ((Collection<ITxGraphPoint>) this._model).Insert(index1, point);
        ++index1;
      }
      this.endUpdateModel();
    }
  }

  internal void Remove(ITxGraphPoint point)
  {
    ((Collection<ITxGraphPoint>) this._model).Remove(point);
  }

  internal void RemoveAt(int index) => ((Collection<ITxGraphPoint>) this._model).RemoveAt(index);

  internal void RemoveRange(IEnumerable<ITxGraphPoint> points)
  {
    this.beginUpdateModel();
    foreach (ITxGraphPoint point in points)
      this.Remove(point);
    this.endUpdateModel();
  }

  internal void RemoveRange(int index, int count)
  {
    if (index < 0 || index > this.Count)
      throw new ArgumentOutOfRangeException(nameof (index));
    if (count < 0 || index + count > this.Count)
      throw new ArgumentOutOfRangeException(nameof (count));
    if (index == 0 && count == this.Count)
    {
      this.RemoveAll();
    }
    else
    {
      if (count <= 0)
        return;
      this.beginUpdateModel();
      for (int index1 = 0; index1 < count; ++index1)
        ((Collection<ITxGraphPoint>) this._model).RemoveAt(index);
      this.endUpdateModel();
    }
  }

  internal void RemoveAll() => ((Collection<ITxGraphPoint>) this._model).Clear();

  internal void Swap(int firstIndex, int secondIndex)
  {
    if (firstIndex < 0 || firstIndex >= this.Count)
      throw new ArgumentOutOfRangeException(nameof (firstIndex));
    if (secondIndex < 0 || secondIndex >= this.Count)
      throw new ArgumentOutOfRangeException(nameof (secondIndex));
    this.beginUpdateModel();
    ITxGraphPoint itxGraphPoint = ((Collection<ITxGraphPoint>) this._model)[firstIndex];
    ((Collection<ITxGraphPoint>) this._model)[firstIndex] = ((Collection<ITxGraphPoint>) this._model)[secondIndex];
    ((Collection<ITxGraphPoint>) this._model)[secondIndex] = itxGraphPoint;
    this.endUpdateModel();
  }

  internal void MoveUp(List<int> items)
  {
    List<int> intList = new List<int>((IEnumerable<int>) items);
    intList.Sort(new Comparison<int>(this.sort_Ascending));
    this.beginUpdateModel();
    foreach (int firstIndex in intList)
      this.Swap(firstIndex, firstIndex - 1);
    this.endUpdateModel();
  }

  internal void MoveDown(List<int> items)
  {
    List<int> intList = new List<int>((IEnumerable<int>) items);
    intList.Sort(new Comparison<int>(this.sort_Descending));
    this.beginUpdateModel();
    foreach (int firstIndex in intList)
      this.Swap(firstIndex, firstIndex + 1);
    this.endUpdateModel();
  }

  internal int SelectionCount
  {
    get
    {
      int selectionCount = 0;
      foreach (ITxGraphPoint itxGraphPoint in (Collection<ITxGraphPoint>) this._model)
      {
        if (itxGraphPoint is ITxGraphSelectablePoint && (itxGraphPoint as ITxGraphSelectablePoint).Selected)
          ++selectionCount;
      }
      return selectionCount;
    }
  }

  internal IEnumerable<ITxGraphPoint> Selection
  {
    get
    {
      List<ITxGraphPoint> selection = new List<ITxGraphPoint>(this.Count);
      foreach (ITxGraphPoint itxGraphPoint in (Collection<ITxGraphPoint>) this._model)
      {
        if (itxGraphPoint is ITxGraphSelectablePoint && (itxGraphPoint as ITxGraphSelectablePoint).Selected)
          selection.Add(itxGraphPoint);
      }
      return (IEnumerable<ITxGraphPoint>) selection;
    }
    set
    {
      if (value == null)
      {
        this.ClearSelection();
      }
      else
      {
        this.beginUpdateSelection();
        foreach (ITxGraphPoint itxGraphPoint in this.Selection)
        {
          if (!value.Contains<ITxGraphPoint>(itxGraphPoint) && itxGraphPoint is ITxGraphSelectablePoint)
            (itxGraphPoint as ITxGraphSelectablePoint).Selected = false;
        }
        foreach (ITxGraphPoint itxGraphPoint in value)
        {
          if (itxGraphPoint is ITxGraphSelectablePoint)
            (itxGraphPoint as ITxGraphSelectablePoint).Selected = true;
        }
        this.endUpdateSelection();
      }
    }
  }

  internal void ClearSelection()
  {
    this.beginUpdateSelection();
    foreach (ITxGraphPoint itxGraphPoint in (Collection<ITxGraphPoint>) this._model)
    {
      if (itxGraphPoint is ITxGraphSelectablePoint)
        (itxGraphPoint as ITxGraphSelectablePoint).Selected = false;
    }
    this.endUpdateSelection();
  }

  internal ITxGraphPoint FocusItem
  {
    get
    {
      if (this._focusItem != null && !((Collection<ITxGraphPoint>) this._model).Contains(this._focusItem))
        this._focusItem = (ITxGraphPoint) null;
      return this._focusItem;
    }
    set
    {
      if (this.FocusItem == value)
        return;
      this._focusItem = value;
      this.fireFocusItemChanged();
    }
  }

  internal string IO_XName => this.IO_getNameX();

  internal string IO_YName => this.IO_getNameY();

  internal void IO_Reset() => this.IO_initModelData();

  internal void IO_Save() => this.IO_storeModelData();

  private void IO_initModelInfo()
  {
    if (!(this._ioData is TxJointVariableLimitsParametersEx ioData))
      return;
    TxJoint joint = ioData.Joint;
    if (TxJoint.op_Inequality(joint, (ITxObject) null))
    {
      TxJoint.TxJointType type = joint.Type;
      if (type != 1)
      {
        if (type == 2)
          this._xDataType = CApJVLEditorViewModel.eDataType.Prismatic;
      }
      else
        this._xDataType = CApJVLEditorViewModel.eDataType.Revolute;
      switch ((int) joint.HardLimitsData.HardLimitsType)
      {
        case 0:
        case 2:
          this._graphDefaultMinX = this.toCurrentUnits(this.getGraphDefaultMinValueInBaseUnits(this._xDataType), this._xDataType);
          this._graphDefaultMaxX = this.toCurrentUnits(this.getGraphDefaultMaxValueInBaseUnits(this._xDataType), this._xDataType);
          break;
        case 1:
          this._graphDefaultMinX = this.toCurrentUnits(joint.HardLimitsData.LowerHardLimit, this._xDataType);
          this._graphDefaultMaxX = this.toCurrentUnits(joint.HardLimitsData.UpperHardLimit, this._xDataType);
          break;
      }
    }
    TxJoint.TxJointType editedJointType = ioData.EditedJointType;
    if (editedJointType != 1)
    {
      if (editedJointType == 2)
        this._yDataType = CApJVLEditorViewModel.eDataType.Prismatic;
    }
    else
      this._yDataType = CApJVLEditorViewModel.eDataType.Revolute;
    this._graphDefaultMinY = this.toCurrentUnits(this.getGraphDefaultMinValueInBaseUnits(this._yDataType), this._yDataType);
    this._graphDefaultMaxY = this.toCurrentUnits(this.getGraphDefaultMaxValueInBaseUnits(this._yDataType), this._yDataType);
  }

  private void IO_initModelData()
  {
    List<ITxGraphPoint> points = new List<ITxGraphPoint>();
    if (this._ioData is TxJointVariableLimitsParametersEx ioData)
    {
      List<double> limitsLeadValues = ioData.VariableLimitsLeadValues;
      List<double> limitsDependValues = ioData.VariableLimitsDependValues;
      if (limitsLeadValues != null && limitsDependValues != null && limitsLeadValues.Count == limitsDependValues.Count)
      {
        for (int index = 0; index < limitsLeadValues.Count; ++index)
        {
          double currentUnits1 = this.toCurrentUnits(limitsLeadValues[index], this._xDataType);
          double currentUnits2 = this.toCurrentUnits(limitsDependValues[index], this._yDataType);
          points.Add(this._model.CreatePoint(currentUnits1, currentUnits2));
        }
      }
    }
    this.SetRange((IEnumerable<ITxGraphPoint>) points);
  }

  private void IO_storeModelData()
  {
    if (!(this._ioData is TxJointVariableLimitsParametersEx ioData))
      return;
    List<double> doubleList1 = new List<double>(((Collection<ITxGraphPoint>) this._model).Count);
    List<double> doubleList2 = new List<double>(((Collection<ITxGraphPoint>) this._model).Count);
    foreach (ITxGraphPoint itxGraphPoint in (Collection<ITxGraphPoint>) this._model)
    {
      double baseUnits1 = this.toBaseUnits(itxGraphPoint.X, this._xDataType);
      double baseUnits2 = this.toBaseUnits(itxGraphPoint.Y, this._yDataType);
      doubleList1.Add(baseUnits1);
      doubleList2.Add(baseUnits2);
    }
    ioData.VariableLimitsLeadValues = doubleList1;
    ioData.VariableLimitsDependValues = doubleList2;
  }

  private string IO_getNameX()
  {
    string nameX = string.Empty;
    if (this._ioData is TxJointVariableLimitsParametersEx ioData)
    {
      TxJoint joint = ioData.Joint;
      if (TxJoint.op_Inequality(joint, (ITxObject) null))
        nameX = joint.Name;
    }
    return nameX;
  }

  private string IO_getNameY()
  {
    string nameY = string.Empty;
    if (this._ioData is TxJointVariableLimitsParametersEx ioData)
      nameY = ioData.EditedJointName;
    return nameY;
  }

  private void connectGraph(CUiJVLEditorGraph graph)
  {
    if (this._connectedControls.Contains((Control) graph))
      return;
    this._connectedControls.Add((Control) graph);
    graph.BeginUpdate();
    ((ITxGraphUpdate) graph.Grid).BeginUpdate();
    graph.Grid.XAxisText = this.getGraphXAxisText();
    graph.Grid.YAxisText = this.getGraphYAxisText();
    graph.Grid.XAxisPadding = new int?(5);
    graph.Grid.YAxisPadding = new int?(5);
    graph.Grid.LinesVisibility = new GridLinesVisibilityType?((GridLinesVisibilityType) 3);
    graph.Grid.ForeColor = new Color?(Color.FromArgb(128 /*0x80*/, 128 /*0x80*/, 128 /*0x80*/));
    graph.Grid.BackColor = new Color?(Color.FromArgb(225, 225, 225));
    this.setGraphAxisDefaultLimits(graph);
    ((ITxGraphUpdate) graph.Grid).EndUpdate();
    ((ITxGraphUpdate) graph.ImageAdjust).BeginUpdate();
    graph.ImageAdjust.Right = new int?(-100);
    graph.ImageAdjust.Top = new int?(100);
    ((ITxGraphUpdate) graph.ImageAdjust).EndUpdate();
    ((ITxGraphUpdate) graph.CustomAxis).BeginUpdate();
    graph.CustomAxis.X = 0.0;
    graph.CustomAxis.Y = 0.0;
    graph.CustomAxis.Color = new Color?(Color.FromArgb(0, 0, 0));
    graph.CustomAxis.Visible = true;
    ((ITxGraphUpdate) graph.CustomAxis).EndUpdate();
    ((ITxGraphUpdate) graph.DataPresentation).BeginUpdate();
    graph.DataPresentation.Visualization = (VisualizationType) 7;
    graph.DataPresentation.LinePresentation.ClosePolyLine = new bool?(true);
    this.applyGraphCustomDataPresentationSettings(graph);
    ((ITxGraphUpdate) graph.DataPresentation).EndUpdate();
    ((ITxGraphUpdate) graph.CursorPrompt).BeginUpdate();
    graph.CursorPrompt.Tracking = true;
    graph.CursorPrompt.Location = new CursorPromptLocationType?((CursorPromptLocationType) 1);
    graph.CursorPrompt.Style = new CursorPromptStyleType?((CursorPromptStyleType) 3);
    ((ITxGraphUpdate) graph.CursorPrompt).EndUpdate();
    graph.PointSize = (PointSizeType) 2;
    graph.MimimumPointSize = (MinimumPointSizeType) 2;
    graph.DataHotSpot = true;
    graph.Title = string.Empty;
    graph.SubTitle = string.Empty;
    ((Control) graph).ForeColor = Color.FromArgb(0, 0, 0);
    ((Control) graph).BackColor = Color.FromArgb((int) byte.MaxValue, (int) byte.MaxValue, (int) byte.MaxValue);
    graph.AllowZoom = true;
    if (graph.DataLayersCount > 0)
      graph.RemoveDataLayers();
    graph.Xnull = 3.4028234663852886E+38;
    graph.Ynull = 3.4028234663852886E+38;
    graph.DataLayerPointsCapacity = 50;
    graph.AddDataLayer();
    graph[0].DataSource = (ITxGraphDataSource) this._model;
    this.updateGraphAxisLimits(graph);
    graph.EndUpdate();
  }

  private void disconnectGraph(CUiJVLEditorGraph graph)
  {
    if (!this._connectedControls.Contains((Control) graph))
      return;
    this._connectedControls.Remove((Control) graph);
    graph.BeginUpdate();
    if (graph.DataLayersCount > 0)
      graph.RemoveDataLayers();
    graph.EndUpdate();
  }

  private void beginUpdateGraph(CUiJVLEditorGraph graph) => graph.BeginUpdate();

  private void endUpdateGraph(CUiJVLEditorGraph graph) => graph.EndUpdate();

  private string getGraphXAxisText()
  {
    return string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.COLUMN_NAME_FIXED_JOINT, (object) this.IO_getNameX(), (object) this.getUnitsName(this._xDataType));
  }

  private string getGraphYAxisText()
  {
    return string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.COLUMN_NAME_VARIABLE_JOINT, (object) this.IO_getNameY(), (object) this.getUnitsName(this._yDataType));
  }

  private void applyGraphCustomDataPresentationSettings(CUiJVLEditorGraph graph)
  {
    ((ITxGraphUpdate) graph.DataPresentation).BeginUpdate();
    graph.DataPresentation.RegularPointPresentation.Shape = new PointShapeType?(this._settings.Graph.RegularPointShape);
    graph.DataPresentation.RegularPointPresentation.Color = new Color?(this._settings.Graph.RegularPointColor);
    graph.DataPresentation.SelectedPointPresentation.Shape = new PointShapeType?(this._settings.Graph.SelectedPointShape);
    graph.DataPresentation.SelectedPointPresentation.Color = new Color?(this._settings.Graph.SelectedPointColor);
    graph.DataPresentation.LinePresentation.Style = new LineType?(this._settings.Graph.LineType);
    graph.DataPresentation.LinePresentation.Color = new Color?(this._settings.Graph.LineColor);
    graph.DataPresentation.LinePresentation.ClosePolyLine = new bool?(true);
    Color areaColor = this._settings.Graph.AreaColor;
    graph.DataPresentation.AreaPresentation.Color = new Color?(Color.FromArgb(64 /*0x40*/, (int) areaColor.R, (int) areaColor.G, (int) areaColor.B));
    ((ITxGraphUpdate) graph.DataPresentation).EndUpdate();
  }

  private void updateGraphAxisLimits(CUiJVLEditorGraph graph)
  {
    if (((Collection<ITxGraphPoint>) this._model).Count > 0)
    {
      double minValue1 = this._graphDefaultMinX;
      double maxValue1 = this._graphDefaultMaxX;
      double minValue2 = this._graphDefaultMinY;
      double maxValue2 = this._graphDefaultMaxY;
      foreach (ITxGraphPoint itxGraphPoint in (Collection<ITxGraphPoint>) this._model)
      {
        if (itxGraphPoint.X < minValue1)
          minValue1 = itxGraphPoint.X;
        else if (itxGraphPoint.X > maxValue1)
          maxValue1 = itxGraphPoint.X;
        if (itxGraphPoint.Y < minValue2)
          minValue2 = itxGraphPoint.Y;
        else if (itxGraphPoint.Y > maxValue2)
          maxValue2 = itxGraphPoint.Y;
      }
      this.applyGraphAxisPadding(ref minValue1, ref maxValue1);
      this.applyGraphAxisPadding(ref minValue2, ref maxValue2);
      ((ITxGraphUpdate) graph.Grid).BeginUpdate();
      graph.Grid.MinX = new double?(minValue1);
      graph.Grid.MaxX = new double?(maxValue1);
      graph.Grid.MinY = new double?(minValue2);
      graph.Grid.MaxY = new double?(maxValue2);
      ((ITxGraphUpdate) graph.Grid).EndUpdate();
    }
    else
      this.setGraphAxisDefaultLimits(graph);
  }

  private void updateGraphAxisLimits(
    CUiJVLEditorGraph graph,
    CApJVLEditorViewModel_ListChangedEventArgs e)
  {
    switch (e.ListChangedType)
    {
      case CApJVLEditorViewModel_ListChangedType.Reset:
      case CApJVLEditorViewModel_ListChangedType.ItemAdded:
      case CApJVLEditorViewModel_ListChangedType.ItemDeleted:
      case CApJVLEditorViewModel_ListChangedType.ItemChanged:
        this.updateGraphAxisLimits(graph);
        break;
    }
  }

  private void updateGraphAxisLimits(
    CUiJVLEditorGraph graph,
    TxGraph_ListItemPropertyChangedEventArgs e)
  {
    string propertyName = e.PropertyName;
    if (!(propertyName == "X") && !(propertyName == "Y"))
      return;
    this.updateGraphAxisLimits(graph);
  }

  private void setGraphAxisDefaultLimits(CUiJVLEditorGraph graph)
  {
    double graphDefaultMinX = this._graphDefaultMinX;
    double graphDefaultMaxX = this._graphDefaultMaxX;
    double graphDefaultMinY = this._graphDefaultMinY;
    double graphDefaultMaxY = this._graphDefaultMaxY;
    this.applyGraphAxisPadding(ref graphDefaultMinX, ref graphDefaultMaxX);
    this.applyGraphAxisPadding(ref graphDefaultMinY, ref graphDefaultMaxY);
    ((ITxGraphUpdate) graph.Grid).BeginUpdate();
    graph.Grid.MinX = new double?(graphDefaultMinX);
    graph.Grid.MaxX = new double?(graphDefaultMaxX);
    graph.Grid.MinY = new double?(graphDefaultMinY);
    graph.Grid.MaxY = new double?(graphDefaultMaxY);
    ((ITxGraphUpdate) graph.Grid).EndUpdate();
  }

  private void applyGraphAxisPadding(ref double minValue, ref double maxValue)
  {
    double num1 = Math.Abs(maxValue - minValue);
    double num2 = 0.05;
    if (num1 < 1E-06)
      num1 = 2.0 * num2 * Math.Abs(maxValue);
    if (minValue <= maxValue)
    {
      minValue -= num1 * num2;
      maxValue += num1 * num2;
    }
    else
    {
      minValue += num1 * num2;
      maxValue -= num1 * num2;
    }
  }

  private double getGraphDefaultMinValueInBaseUnits(CApJVLEditorViewModel.eDataType dataType)
  {
    double valueInBaseUnits = 0.0;
    switch (dataType)
    {
      case CApJVLEditorViewModel.eDataType.Revolute:
        valueInBaseUnits = -1.0 * Math.PI;
        break;
      case CApJVLEditorViewModel.eDataType.Prismatic:
        valueInBaseUnits = -100.0;
        break;
    }
    return valueInBaseUnits;
  }

  private double getGraphDefaultMaxValueInBaseUnits(CApJVLEditorViewModel.eDataType dataType)
  {
    double valueInBaseUnits = 0.0;
    switch (dataType)
    {
      case CApJVLEditorViewModel.eDataType.Revolute:
        valueInBaseUnits = Math.PI;
        break;
      case CApJVLEditorViewModel.eDataType.Prismatic:
        valueInBaseUnits = 100.0;
        break;
    }
    return valueInBaseUnits;
  }

  private string getGridColumnFormat(CApJVLEditorViewModel.eDataType type)
  {
    int afterDecimalPoint = this.getDigitsAfterDecimalPoint(type);
    return afterDecimalPoint > 0 ? "0." + new string('0', afterDecimalPoint) : "0";
  }

  private string getGridColumnCaption(string columnName)
  {
    string gridColumnCaption = string.Empty;
    switch (columnName)
    {
      case "N:":
        gridColumnCaption = DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.COLUMN_NAME_ITEM_NUMBER;
        break;
      case "X":
        gridColumnCaption = string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.COLUMN_NAME_FIXED_JOINT, (object) this.IO_getNameX(), (object) this.getUnitsName(this._xDataType));
        break;
      case "Y":
        gridColumnCaption = string.Format(DnProcessSimulateCommands.JointVariableLimitsEditor.Resources.StringTable.COLUMN_NAME_VARIABLE_JOINT, (object) this.IO_getNameY(), (object) this.getUnitsName(this._yDataType));
        break;
    }
    return gridColumnCaption;
  }

  private void connectGrid(CUiJVLEditorGrid grid)
  {
    if (this._connectedControls.Contains((Control) grid))
      return;
    this._connectedControls.Add((Control) grid);
    ((ScrollableControl) grid).BeginUpdate();
    ((C1FlexGridBase) grid).SelectionMode = (SelectionModeEnum) 7;
    ((C1FlexGridBase) grid).AllowSorting = (AllowSortingEnum) 0;
    ((C1FlexGridBase) grid).AllowResizing = (AllowResizingEnum) 1;
    ((C1FlexGridBase) grid).AllowEditing = true;
    ((C1FlexGridBase) grid).AllowFiltering = false;
    ((C1.Win.C1FlexGrid.C1FlexGrid) grid).AllowMerging = (AllowMergingEnum) 0;
    ((C1FlexGridBase) grid).AllowDragging = (AllowDraggingEnum) 0;
    ((C1FlexGridBase) grid).AllowDelete = false;
    ((ScrollableControl) grid).ScrollBars = ScrollBars.Both;
    ((C1FlexGridBase) grid).ExtendLastCol = true;
    ((C1FlexGridBase) grid).AutoResize = false;
    ((C1FlexGridBase) grid).Styles.Frozen.BackColor = ((C1FlexGridBase) grid).Styles.Fixed.BackColor;
    string[] strArray = new string[3]{ "N:", "X", "Y" };
    ((RowColCollection) ((C1FlexGridBase) grid).Rows).Count = 1;
    ((RowColCollection) ((C1FlexGridBase) grid).Rows).Fixed = 1;
    ((RowColCollection) ((C1FlexGridBase) grid).Cols).Count = strArray.Length;
    ((RowColCollection) ((C1FlexGridBase) grid).Cols).Fixed = 0;
    ((RowColCollection) ((C1FlexGridBase) grid).Cols).Frozen = 1;
    foreach (Column col in (IEnumerable) ((C1FlexGridBase) grid).Cols)
    {
      string columnName = strArray[((RowCol) col).Index];
      col.Name = columnName;
      col.Caption = this.getGridColumnCaption(columnName);
      switch (columnName)
      {
        case "N:":
          ((RowCol) col).DataType = typeof (string);
          ((RowCol) col).Format = string.Empty;
          ((RowCol) col).TextAlign = (TextAlignEnum) 4;
          ((RowCol) col).TextAlignFixed = (TextAlignEnum) 4;
          ((RowCol) col).AllowEditing = false;
          ((C1FlexGridBase) grid).AutoSizeCol(((RowCol) col).Index);
          if (col.WidthDisplay < 20)
          {
            col.WidthDisplay = 20;
            continue;
          }
          continue;
        case "X":
          ((RowCol) col).DataType = typeof (double);
          ((RowCol) col).Format = this.getGridColumnFormat(this._xDataType);
          ((RowCol) col).TextAlign = (TextAlignEnum) 1;
          ((RowCol) col).TextAlignFixed = (TextAlignEnum) 1;
          ((RowCol) col).AllowEditing = true;
          ((C1FlexGridBase) grid).AutoSizeCol(((RowCol) col).Index);
          int num1 = (((Control) grid).Width - ((C1FlexGridBase) grid).Cols[0].WidthDisplay - 4) / (strArray.Length - 1);
          if (num1 < col.WidthDisplay)
            num1 = col.WidthDisplay;
          if (num1 < 75)
            num1 = 75;
          col.WidthDisplay = num1;
          continue;
        case "Y":
          ((RowCol) col).DataType = typeof (double);
          ((RowCol) col).Format = this.getGridColumnFormat(this._yDataType);
          ((RowCol) col).TextAlign = (TextAlignEnum) 1;
          ((RowCol) col).TextAlignFixed = (TextAlignEnum) 1;
          ((RowCol) col).AllowEditing = true;
          ((C1FlexGridBase) grid).AutoSizeCol(((RowCol) col).Index);
          int num2 = (((Control) grid).Width - ((C1FlexGridBase) grid).Cols[0].WidthDisplay - 4) / (strArray.Length - 1);
          if (num2 < col.WidthDisplay)
            num2 = col.WidthDisplay;
          if (num2 < 85)
            num2 = 85;
          col.WidthDisplay = num2;
          continue;
        default:
          continue;
      }
    }
    this.applyGridCustomSettings(grid);
    ((ScrollableControl) grid).EndUpdate();
  }

  private void disconnectGrid(CUiJVLEditorGrid grid)
  {
    if (!this._connectedControls.Contains((Control) grid))
      return;
    this._connectedControls.Remove((Control) grid);
    ((ScrollableControl) grid).BeginUpdate();
    ((RowColCollection) ((C1FlexGridBase) grid).Rows).Count = 0;
    ((RowColCollection) ((C1FlexGridBase) grid).Cols).Count = 0;
    ((ScrollableControl) grid).EndUpdate();
  }

  private void beginUpdateGrid(CUiJVLEditorGrid grid) => ((ScrollableControl) grid).BeginUpdate();

  private void endUpdateGrid(CUiJVLEditorGrid grid) => ((ScrollableControl) grid).EndUpdate();

  private void applyGridCustomSettings(CUiJVLEditorGrid grid)
  {
    ((ScrollableControl) grid).BeginUpdate();
    if (((C1FlexGridBase) grid).Cols.Contains("N:"))
    {
      int num = (int) (this._settings.Grid.NColumnWidthRatio * (double) ((Control) grid).Width);
      if (num < 20)
        num = 20;
      ((C1FlexGridBase) grid).Cols["N:"].WidthDisplay = num;
    }
    if (((C1FlexGridBase) grid).Cols.Contains("X"))
    {
      int num = (int) (this._settings.Grid.XColumnWidthRatio * (double) ((Control) grid).Width);
      if (num < 75)
        num = 75;
      ((C1FlexGridBase) grid).Cols["X"].WidthDisplay = num;
    }
    if (((C1FlexGridBase) grid).Cols.Contains("Y"))
    {
      int num = (int) (this._settings.Grid.YColumnWidthRatio * (double) ((Control) grid).Width);
      if (num < 85)
        num = 85;
      ((C1FlexGridBase) grid).Cols["Y"].WidthDisplay = num;
    }
    ((ScrollableControl) grid).EndUpdate();
  }

  private void updateGridCustomSettings(CUiJVLEditorGrid grid)
  {
    if (((C1FlexGridBase) grid).Cols.Contains("N:"))
    {
      int num = ((C1FlexGridBase) grid).Cols["N:"].WidthDisplay;
      if (num < 20)
        num = 20;
      this._settings.Grid.NColumnWidthRatio = (double) num / (double) ((Control) grid).Width;
    }
    if (((C1FlexGridBase) grid).Cols.Contains("X"))
    {
      int num = ((C1FlexGridBase) grid).Cols["X"].WidthDisplay;
      if (num < 75)
        num = 75;
      this._settings.Grid.XColumnWidthRatio = (double) num / (double) ((Control) grid).Width;
    }
    if (!((C1FlexGridBase) grid).Cols.Contains("Y"))
      return;
    int num1 = ((C1FlexGridBase) grid).Cols["Y"].WidthDisplay;
    if (num1 < 85)
      num1 = 85;
    this._settings.Grid.YColumnWidthRatio = (double) num1 / (double) ((Control) grid).Width;
  }

  internal CUiJVLEditorViewModelSettings Settings => this._settings;

  internal bool LoadSettings(Form form)
  {
    CUiJVLEditorViewModelSettings viewModelSettings = new CUiJVLEditorViewModelSettings(this._settings);
    string s;
    bool flag1 = this.loadSettingsValue(form, "GraphSettings", "RegularPointShape", out s);
    if (flag1)
    {
      PointShapeType pointShapeType;
      flag1 = this.fromSettingsString(s, out pointShapeType);
      if (flag1)
        viewModelSettings.Graph.RegularPointShape = pointShapeType;
    }
    bool flag2 = flag1 && this.loadSettingsValue(form, "GraphSettings", "RegularPointColor", out s);
    if (flag2)
    {
      Color color;
      flag2 = this.fromSettingsString(s, out color);
      if (flag2)
        viewModelSettings.Graph.RegularPointColor = color;
    }
    bool flag3 = flag2 && this.loadSettingsValue(form, "GraphSettings", "SelectedPointShape", out s);
    if (flag3)
    {
      PointShapeType pointShapeType;
      flag3 = this.fromSettingsString(s, out pointShapeType);
      if (flag3)
        viewModelSettings.Graph.SelectedPointShape = pointShapeType;
    }
    bool flag4 = flag3 && this.loadSettingsValue(form, "GraphSettings", "SelectedPointColor", out s);
    if (flag4)
    {
      Color color;
      flag4 = this.fromSettingsString(s, out color);
      if (flag4)
        viewModelSettings.Graph.SelectedPointColor = color;
    }
    bool flag5 = flag4 && this.loadSettingsValue(form, "GraphSettings", "LineType", out s);
    if (flag5)
    {
      LineType lineType;
      flag5 = this.fromSettingsString(s, out lineType);
      if (flag5)
        viewModelSettings.Graph.LineType = lineType;
    }
    bool flag6 = flag5 && this.loadSettingsValue(form, "GraphSettings", "LineColor", out s);
    if (flag6)
    {
      Color color;
      flag6 = this.fromSettingsString(s, out color);
      if (flag6)
        viewModelSettings.Graph.LineColor = color;
    }
    bool flag7 = flag6 && this.loadSettingsValue(form, "GraphSettings", "AreaColor", out s);
    if (flag7)
    {
      Color color;
      flag7 = this.fromSettingsString(s, out color);
      if (flag7)
        viewModelSettings.Graph.AreaColor = color;
    }
    bool flag8 = flag7 && this.loadSettingsValue(form, "GridSettings", "NColumnWidthRatio", out s);
    if (flag8)
    {
      double num;
      flag8 = this.fromSettingsString(s, out num);
      if (flag8)
        viewModelSettings.Grid.NColumnWidthRatio = num;
    }
    bool flag9 = flag8 && this.loadSettingsValue(form, "GridSettings", "XColumnWidthRatio", out s);
    if (flag9)
    {
      double num;
      flag9 = this.fromSettingsString(s, out num);
      if (flag9)
        viewModelSettings.Grid.XColumnWidthRatio = num;
    }
    bool flag10 = flag9 && this.loadSettingsValue(form, "GridSettings", "YColumnWidthRatio", out s);
    if (flag10)
    {
      double num;
      flag10 = this.fromSettingsString(s, out num);
      if (flag10)
        viewModelSettings.Grid.YColumnWidthRatio = num;
    }
    bool flag11 = flag10 && this.loadSettingsValue(form, "FormSettings", "SplitterPosRatio", out s);
    if (flag11)
    {
      double num;
      flag11 = this.fromSettingsString(s, out num);
      if (flag11)
        viewModelSettings.SplitterPosRatio = num;
    }
    if (flag11)
      this._settings = viewModelSettings;
    return flag11;
  }

  internal bool StoreSettings(Form form)
  {
    return this.storeSettingsValue(form, "GraphSettings", "RegularPointShape", this.toSettingsString(this._settings.Graph.RegularPointShape)) && this.storeSettingsValue(form, "GraphSettings", "RegularPointColor", this.toSettingsString(this._settings.Graph.RegularPointColor)) && this.storeSettingsValue(form, "GraphSettings", "SelectedPointShape", this.toSettingsString(this._settings.Graph.SelectedPointShape)) && this.storeSettingsValue(form, "GraphSettings", "SelectedPointColor", this.toSettingsString(this._settings.Graph.SelectedPointColor)) && this.storeSettingsValue(form, "GraphSettings", "LineType", this.toSettingsString(this._settings.Graph.LineType)) && this.storeSettingsValue(form, "GraphSettings", "LineColor", this.toSettingsString(this._settings.Graph.LineColor)) && this.storeSettingsValue(form, "GraphSettings", "AreaColor", this.toSettingsString(this._settings.Graph.AreaColor)) && this.storeSettingsValue(form, "GridSettings", "NColumnWidthRatio", this.toSettingsString(this._settings.Grid.NColumnWidthRatio)) && this.storeSettingsValue(form, "GridSettings", "XColumnWidthRatio", this.toSettingsString(this._settings.Grid.XColumnWidthRatio)) && this.storeSettingsValue(form, "GridSettings", "YColumnWidthRatio", this.toSettingsString(this._settings.Grid.YColumnWidthRatio)) && this.storeSettingsValue(form, "FormSettings", "SplitterPosRatio", this.toSettingsString(this._settings.SplitterPosRatio));
  }

  internal void ApplyGraphSettings()
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGraph)
        this.applyGraphCustomDataPresentationSettings(connectedControl as CUiJVLEditorGraph);
    }
  }

  internal void ApplyGridSettings(
    CUiJVLEditorViewModelSettings.GridSettings gridSettings)
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGrid)
        this.applyGridCustomSettings(connectedControl as CUiJVLEditorGrid);
    }
  }

  internal void UpdateGridSettings()
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGrid)
        this.updateGridCustomSettings(connectedControl as CUiJVLEditorGrid);
    }
  }

  private bool loadSettingsValue(
    Form form,
    string collectionRelativePath,
    string propName,
    out string value)
  {
    bool flag = false;
    value = (string) null;
    try
    {
      object obj;
      TxFormSettings.RestoreFormValue(form, collectionRelativePath, propName, ref obj);
      if (obj != null)
      {
        value = obj.ToString();
        flag = true;
      }
    }
    catch (Exception ex)
    {
      value = (string) null;
      flag = false;
    }
    return flag;
  }

  private bool storeSettingsValue(
    Form form,
    string collectionRelativePath,
    string propName,
    string value)
  {
    try
    {
      TxFormSettings.StoreFormValue(form, collectionRelativePath, propName, value);
      return true;
    }
    catch (Exception ex)
    {
      return false;
    }
  }

  private string toSettingsString(double value)
  {
    CultureInfo provider = new CultureInfo("en-US");
    return value.ToString((IFormatProvider) provider);
  }

  private bool fromSettingsString(string s, out double value)
  {
    CultureInfo provider = new CultureInfo("en-US");
    return double.TryParse(s, NumberStyles.AllowDecimalPoint, (IFormatProvider) provider, out value);
  }

  private string toSettingsString(Color color) => color.ToArgb().ToString();

  private bool fromSettingsString(string s, out Color value)
  {
    value = Color.Empty;
    int result = 0;
    if (!int.TryParse(s, out result))
      return false;
    value = Color.FromArgb(result);
    return true;
  }

  private string toSettingsString(PointShapeType pointShape)
  {
    string empty = string.Empty;
    string settingsString;
    switch (pointShape - 2)
    {
      case 0:
        settingsString = "Dot";
        break;
      case 1:
        settingsString = "DotSolid";
        break;
      case 2:
        settingsString = "Square";
        break;
      case 3:
        settingsString = "SquareSolid";
        break;
      case 4:
        settingsString = "Diamond";
        break;
      case 5:
        settingsString = "DiamondSolid";
        break;
      default:
        settingsString = "Default";
        break;
    }
    return settingsString;
  }

  private bool fromSettingsString(string s, out PointShapeType value)
  {
    bool flag = true;
    // ISSUE: cast to a reference type
    // ISSUE: explicit reference operation
    ^(int&) ref value = 5;
    switch (s)
    {
      case "Diamond":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 6;
        break;
      case "DiamondSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 7;
        break;
      case "Dot":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 2;
        break;
      case "DotSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 3;
        break;
      case "Square":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 4;
        break;
      case "SquareSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 5;
        break;
      default:
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 5;
        break;
    }
    return flag;
  }

  private string toSettingsString(LineType lineType)
  {
    string empty = string.Empty;
    string settingsString;
    if (lineType != null)
    {
      switch (lineType - 5)
      {
        case 0:
          settingsString = "MediumSolid";
          break;
        case 1:
          settingsString = "ThickSolid";
          break;
        case 4:
          settingsString = "MediumThinSolid";
          break;
        case 5:
          settingsString = "MediumThickSolid";
          break;
        default:
          settingsString = "Default";
          break;
      }
    }
    else
      settingsString = "ThinSolid";
    return settingsString;
  }

  private bool fromSettingsString(string s, out LineType value)
  {
    bool flag = true;
    // ISSUE: cast to a reference type
    // ISSUE: explicit reference operation
    ^(int&) ref value = 0;
    switch (s)
    {
      case "MediumSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 5;
        break;
      case "MediumThickSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 10;
        break;
      case "MediumThinSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 9;
        break;
      case "ThickSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 6;
        break;
      case "ThinSolid":
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 0;
        break;
      default:
        // ISSUE: cast to a reference type
        // ISSUE: explicit reference operation
        ^(int&) ref value = 0;
        break;
    }
    return flag;
  }

  private void getValidRange(
    CApJVLEditorViewModel.eDataType type,
    out double minValue,
    out double maxValue)
  {
    minValue = 0.0;
    maxValue = 0.0;
    if (type != CApJVLEditorViewModel.eDataType.Revolute)
    {
      if (type != CApJVLEditorViewModel.eDataType.Prismatic)
        return;
      minValue = this.toCurrentUnits(-999999.0, type);
      maxValue = this.toCurrentUnits(999999.0, type);
    }
    else
    {
      minValue = this.toCurrentUnits(-17453.27507, type);
      maxValue = this.toCurrentUnits(17453.27507, type);
    }
  }

  private string getFormatedValue(CApJVLEditorViewModel.eDataType type, double value)
  {
    int afterDecimalPoint = this.getDigitsAfterDecimalPoint(type);
    return afterDecimalPoint > 0 ? string.Format($"{{0:0.{new string('0', afterDecimalPoint)}}}", (object) value) : $"{value:0}";
  }

  private void beginUpdateModel()
  {
    ++this._internalModelUpdateCounter;
    this._model.BeginUpdate();
  }

  private void endUpdateModel()
  {
    if (this._internalModelUpdateCounter > 0)
    {
      --this._internalModelUpdateCounter;
      if (this._internalModelUpdateCounter == 0)
      {
        this.fireListChanged(CApJVLEditorViewModel_ListChangedType.Reset, -1);
        this.updateConnectedControlsAfterModelChanged(new CApJVLEditorViewModel_ListChangedEventArgs(CApJVLEditorViewModel_ListChangedType.Reset, -1));
      }
    }
    this._model.EndUpdate();
  }

  private void beginUpdateSelection()
  {
    ++this._internalSelectionUpdateCounter;
    this._model.BeginUpdate();
  }

  private void endUpdateSelection()
  {
    if (this._internalSelectionUpdateCounter > 0)
    {
      --this._internalSelectionUpdateCounter;
      if (this._internalSelectionUpdateCounter == 0)
        this.fireSelectionChanged();
    }
    this._model.EndUpdate();
  }

  private void registerToModelEvents()
  {
    this._model.UpdateStarted += new EventHandler(this._model_UpdateStarted);
    this._model.UpdateEnded += new EventHandler(this._model_UpdateEnded);
    ((BindingList<ITxGraphPoint>) this._model).ListChanged += new ListChangedEventHandler(this._model_ListChanged);
    this._model.ListItemPropertyChanged += new TxGraph_ListItemPropertyChangedEventHandler(this._model_ListItemPropertyChanged);
  }

  private void unregisterToModelEvents()
  {
    this._model.UpdateStarted -= new EventHandler(this._model_UpdateStarted);
    this._model.UpdateEnded -= new EventHandler(this._model_UpdateEnded);
    ((BindingList<ITxGraphPoint>) this._model).ListChanged -= new ListChangedEventHandler(this._model_ListChanged);
    this._model.ListItemPropertyChanged -= new TxGraph_ListItemPropertyChangedEventHandler(this._model_ListItemPropertyChanged);
  }

  private void updateConnectedControlsAfterModelChanged(CApJVLEditorViewModel_ListChangedEventArgs e)
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGraph)
      {
        this.updateGraphAxisLimits(connectedControl as CUiJVLEditorGraph, e);
      }
      else
      {
        CUiJVLEditorGrid cuiJvlEditorGrid = connectedControl as CUiJVLEditorGrid;
      }
    }
  }

  private void updateConnectedControlsAfterItemPropertyChanged(
    TxGraph_ListItemPropertyChangedEventArgs e)
  {
    foreach (Control connectedControl in this._connectedControls)
    {
      if (connectedControl is CUiJVLEditorGraph)
      {
        this.updateGraphAxisLimits(connectedControl as CUiJVLEditorGraph, e);
      }
      else
      {
        CUiJVLEditorGrid cuiJvlEditorGrid = connectedControl as CUiJVLEditorGrid;
      }
    }
  }

  private void fireUpdateStarted()
  {
    if (this.UpdateStarted == null)
      return;
    this.UpdateStarted((object) this, new EventArgs());
  }

  private void fireUpdateEnded()
  {
    if (this.UpdateEnded == null)
      return;
    this.UpdateEnded((object) this, new EventArgs());
  }

  private void fireListChanged(
    CApJVLEditorViewModel_ListChangedType listChangedType,
    int newIndex)
  {
    if (this.ListChanged == null)
      return;
    this.ListChanged((object) this, new CApJVLEditorViewModel_ListChangedEventArgs(listChangedType, newIndex));
  }

  private void fireListChanged(
    CApJVLEditorViewModel_ListChangedType listChangedType,
    int newIndex,
    int oldIndex)
  {
    if (this.ListChanged == null)
      return;
    this.ListChanged((object) this, new CApJVLEditorViewModel_ListChangedEventArgs(listChangedType, newIndex, oldIndex));
  }

  private void fireListItemPropertyChanged(int itemIndex, string propertyName)
  {
    if (this.ListItemPropertyChanged == null)
      return;
    this.ListItemPropertyChanged((object) this, new CApJVLEditorViewModel_ListItemPropertyChangedEventArgs(itemIndex, propertyName));
  }

  private void fireSelectionChanged()
  {
    if (this.SelectionChanged == null)
      return;
    this.SelectionChanged((object) this, new EventArgs());
  }

  private void fireFocusItemChanged()
  {
    if (this.FocusItemChanged == null)
      return;
    this.FocusItemChanged((object) this, new EventArgs());
  }

  private int sort_Ascending(int first, int second) => first.CompareTo(second);

  private int sort_Descending(int first, int second) => second.CompareTo(first);

  private double getUnitsMultiplier(CApJVLEditorViewModel.eDataType dataType)
  {
    double unitsMultiplier = 1.0;
    switch (dataType)
    {
      case CApJVLEditorViewModel.eDataType.Revolute:
        unitsMultiplier = this._revoluteUnitsMultiplier;
        break;
      case CApJVLEditorViewModel.eDataType.Prismatic:
        unitsMultiplier = this._prismaticUnitsMultiplier;
        break;
    }
    return unitsMultiplier;
  }

  private int getDigitsAfterDecimalPoint(CApJVLEditorViewModel.eDataType dataType)
  {
    int afterDecimalPoint = 0;
    switch (dataType)
    {
      case CApJVLEditorViewModel.eDataType.Revolute:
        afterDecimalPoint = this._revoluteDigitsAfterDecimalPoint;
        break;
      case CApJVLEditorViewModel.eDataType.Prismatic:
        afterDecimalPoint = this._prismaticDigitsAfterDecimalPoint;
        break;
    }
    return afterDecimalPoint;
  }

  private string getUnitsName(CApJVLEditorViewModel.eDataType dataType)
  {
    string unitsName = string.Empty;
    switch (dataType)
    {
      case CApJVLEditorViewModel.eDataType.Revolute:
        unitsName = this._revoluteUnitsName;
        break;
      case CApJVLEditorViewModel.eDataType.Prismatic:
        unitsName = this._prismaticUnitsName;
        break;
    }
    return unitsName;
  }

  private double toCurrentUnits(double baseUnitsValue, CApJVLEditorViewModel.eDataType dataType)
  {
    return baseUnitsValue / this.getUnitsMultiplier(dataType);
  }

  private double toBaseUnits(double currentUnitsValue, CApJVLEditorViewModel.eDataType dataType)
  {
    return this.getUnitsMultiplier(dataType) * currentUnitsValue;
  }

  private void _model_UpdateStarted(object sender, EventArgs e) => this.BeginUpdate();

  private void _model_UpdateEnded(object sender, EventArgs e) => this.EndUpdate();

  private void _model_ListItemPropertyChanged(
    object sender,
    TxGraph_ListItemPropertyChangedEventArgs e)
  {
    if (this._internalModelUpdateCounter != 0)
      return;
    this.fireListItemPropertyChanged(e.ItemIndex, e.PropertyName);
    this.updateConnectedControlsAfterItemPropertyChanged(e);
  }

  private void _model_ListChanged(object sender, ListChangedEventArgs e)
  {
    if (this._internalModelUpdateCounter != 0)
      return;
    CApJVLEditorViewModel_ListChangedType listChangedType = CApJVLEditorViewModelConvertor.ListChangedType(e.ListChangedType);
    this.fireListChanged(listChangedType, e.NewIndex, e.OldIndex);
    this.updateConnectedControlsAfterModelChanged(new CApJVLEditorViewModel_ListChangedEventArgs(listChangedType, e.NewIndex, e.OldIndex));
  }

  private enum eDataType
  {
    Revolute,
    Prismatic,
  }
}
